/** * Global VCMS Javascript framework. * This file need to use together with jquery. * @since 7.4.0 * @author BabyWolf (Tony Nguyen) */ /** * Checking if JQuery is loaded and declare some global functions using Jquery. */ window.onload = function() { if (!window.jQuery) { console.log("Jquery is not included. Please include it before using this script to have full VCMS' Javasript functions."); } else { /** * Jquery global action */ $('.back a').click(function(e) { window.history.back(); e.preventDefault(); }); } } /** * VCMS Object. * @type {Object} */ var $vs = { clone: function($object) { return JSON.parse(JSON.stringify($object)); }, touchHandle: function($callback) { if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) { $('body').addClass('touch'); if(typeof $callback == 'function') { $callback(); } } }, hideMessage: function() { $('.ajax-message').hide('slow'); }, showSuccess: function($message, $title) { if(!$vsSystem.isEmpty($title)) { $('#ajax-success-title').text($title); } $('#ajax-success-msg').html($message); $('#ajax-success').show('slow'); }, showInfo: function($message, $title) { if(!$vsSystem.isEmpty($title)) { $('#ajax-info-title').text($title); } $('#ajax-info-msg').html($message); $('#ajax-info').show('slow'); }, /** * Show error box. * @param {String} $message * @param {String} $title */ showError: function($message, $title) { if(!$vsSystem.isEmpty($title)) { $('#ajax-error-title').text($title); } $('#ajax-error-msg').html($message); $('#ajax-error').show('slow'); }, popUp: function($selector) { if(typeof sessionStorage.poppedUp == 'undefined') sessionStorage.poppedUp = 0; // No need to display if it already displayed if(sessionStorage.poppedUp == 1) return; $($selector).modal(); sessionStorage.poppedUp = 1; }, /** * Ajax calling with get method. * @return {boolean} */ get: function($url, $options) { $.get($url, $options.data) .done(function($response) { if(false == $response.status) { if($vsSystem.isFunction($options.fail)) { $options.fail($response); } } else { if($vsSystem.isFunction($options.success)) { $options.success($response); } return true; } }) .fail(function( $jqXHR, $textStatus ) { var $response = { jqXHR: $jqXHR, message: $textStatus }; $options.fail($response); }); return false; }, /** * Ajax calling with post method. * @return {boolean} */ post: function($url, $options) { $options.method = 'POST'; $.post($url, $options.data) .done(function($response) { if(true == $response.status) { if($vsSystem.isFunction($options.success)) { $options.success($response); } return true; } else { if($vsSystem.isFunction($options.fail)) { $options.fail($response); } } }) .fail(function( $jqXHR, $textStatus ) { var $response = { jqXHR: $jqXHR, message: $textStatus }; $options.fail($response); }); return false; }, baseURL: function() { return window.location.protocol + "//" + window.location.host + "/"; }, /** * Set menus to active state automatically which have class .vs-menu */ autoActivateMenu: function() { if(typeof $activatePath !== 'undefined') { $vs.activateMenu('.vs-menu', $activatePath); return; } $vs.activateMenu('.vs-menu'); }, /** * Set menu to activate state (add css class .active) * @param {selector} $container - The jQuery selector that hold the menu. */ activateMenu: function($container, $path) { if(typeof $path === 'undefined' && $path !== false) $path = window.location.pathname; // Make sure specified path don't have slash at the begining $path = $path.trimLeft('/'); var $item = $($container+' a').filter(function() { return this.pathname == '/'+$path && $(this).attr('href') != '#'; }).parent(); $item.addClass('active'); $item.parentsUntil($container,$item.prop('tagName')).addClass('active') }, /** * Render admin panel. */ AdminPanel: function() { // Checking if variable is defined if (typeof $baseURL == 'undefined' || typeof $path == 'undefined' || typeof $langCode == 'undefined') { console.log("$baseURL, $path or $langCode variable is not defined, please specify it to load Admin Panel"); return false; } // Loading admin panel var $adminPanelURL = $baseURL + 'publicbar?path=' + $path + '&code=' + $langCode; $.get($adminPanelURL).done(function(data) { if (data.status == true) { $('body').append(data.html); $('body').addClass('adminbar-expand'); } else console.log(data.message); }).fail(function(jqXHR, textStatus) { console.log(textStatus); }); } } /** * VS global function call. */ $vs.autoActivateMenu();